﻿using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.DataCollection.Model;
using ZR.DataCollection.OperationCore.OperationEnum;
using ZR.DataCollection.OperationCore.OperationHelper;
using ZR.DataCollection.SingleParameter;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;

namespace ZR.DataCollection.OperationCore.OperationMode
{
    /// <summary>
    /// 请求加工
    /// </summary>
    public class Process : OperationInfoBase
    {
        private bool collectStatus = true;

        public override event Action<string> Event_Notification;

        private readonly IWorkstationConfigService _workstationConfigService = App.GetRequiredService<IWorkstationConfigService>();
        private readonly IStationVerificationConfigService _stationVerificationConfigService = App.GetRequiredService<IStationVerificationConfigService>();
        private readonly IStationWorkInfoService _stationWorkInfoService = App.GetRequiredService<IStationWorkInfoService>();

        public Process(int operationType, string stationType) : base(operationType, stationType)
        {
        }

        public override void WorkDetail(ImpParameter imp)
        {
            DataDock? dataDock = imp.objectParam as DataDock;
            var keyValuePairs = CurrentCruxPoint(dataDock.LineCode, dataDock.ProductType, dataDock.ItemNo);
            SpotHandle(imp, keyValuePairs);
        }

        public override void SpotHandle(ImpParameter parameter, Dictionary<int, List<OperationPointConfig>> SpotGroup)
        {
            foreach (var SpotInfo in SpotGroup)
            {
                switch (SpotInfo.Key)
                {
                    case (int)DataOperateEnum.Read:
                        ReadFunction(parameter, SpotInfo.Value);
                        break;

                    case (int)DataOperateEnum.Write:
                        SpotInfo.Value.ForEach(x =>
                        {
                            if (!collectStatus && x.ItemSort.Equals(3))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, x.ItemWriteValue);
                            }
                            parameter.PLCDetail.WriteValue(x.ItemNo, x.ItemWriteValue);
                        });
                        break;

                    case (int)DataOperateEnum.Verify:
                        break;

                    default:
                        break;
                }
            }
        }

        private void ReadFunction(ImpParameter parameter, List<OperationPointConfig> OperationPointList)
        {
            DataDock dataDock = parameter.objectParam as DataDock;
            foreach (var PointInfo in OperationPointList)
            {
                string? productId = parameter.PLCDetail.ReadValue(PointInfo.ItemNo).ToString();
                if (string.IsNullOrWhiteSpace(productId))
                {
                    collectStatus = false;
                    return;
                }
                StationWorkInfo stationWorkInfo = null;
                bool sve = _workstationConfigService.QueryInfo(dataDock?.LineCode, dataDock?.StationCode).StationVerifyEnabled.Equals(1);
                if (sve)
                {
                    string upperStation = _stationVerificationConfigService.UpperWorkstation(dataDock.LineCode, dataDock.StationCode);
                    stationWorkInfo = _stationWorkInfoService.QueryInfo(dataDock.LineCode, upperStation, productId);
                    if (stationWorkInfo == null)
                    {
                        collectStatus = false;
                        return;
                    }
                    if (stationWorkInfo.WorkResult.Equals("NG"))
                    {
                        collectStatus = false;
                        return;
                    }
                }
                ProductInfoManager.Instance.SetOrderCode(dataDock.StationCode, stationWorkInfo.OrderCode);
                ProductInfoManager.Instance.SetProductNo(dataDock.StationCode, productId);
                ProductInfoManager.Instance.SetStartTime(dataDock.StationCode, DateTime.Now);
            }
        }
    }
}